<?php

	namespace App\Service\Approval\ApprovalCreditProject;

	use App\Service\Approval\DingTalkApprovalInterFace;
    use App\Models\modelCreditProjectManage;

	class ApprovalCreditProjectCreditDepartmentLeaderReview implements DingTalkApprovalInterFace
	{
		public function __construct()
		{
			$this->m_cHelper					= app()->gcHelper					;
			$this->m_modelCreditProjectManage	= new modelCreditProjectManage()	;
		}
		
		/* 审批开始	=> [ 信用项目审批第三节点部门领导审查 ]
		 *
		 */
		public function approvalStart($processInstanceId,$sourceId,$processStatus)
		{
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere	(
																		$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_TABLE_NAME')
																		,
																		["where id=?",[$sourceId]]
																		,
																		[
																			'credit_project_status'	=> modelCreditProjectManage::CREDIT_PROJECT_STATUS_5_REVIEWING
																		]	
																	);

		}

		/* 审批完成 =>	[ 当审批完成后需要再次检查当前信用项目额度是否足够因为存在调增调减问题会影响客户的信用余额 ]
		 *
		 */
		public function approvalAgreeEnd($processInstanceId, $sourceId,$processStatus)
		{
			# get creidt project raw information
			#
			$credit_project					= $this->m_modelCreditProjectManage->getCreditProjectTableRawInformationByID($sourceId/* credit project id */);

			# 当前信用项目的额度是否在正常范围
			#
			#
			if( $this->m_modelCreditProjectManage->verifyClientHaveEnoughRemainCreditAmount($credit_project->credit_project_client_id/* customer id */,$credit_project->credit_project_quota/* credit project quota */))
			{
				$credit_project_status		= modelCreditProjectManage::CREDIT_PROJECT_STATUS_2_REVIEW_OK										;	# 额度足够
			}
			else
			{
				$credit_project_status		= modelCreditProjectManage::CREDIT_PROJECT_STATUS_10_REVIEW_OK_BUT_CUSTOMER_NOT_ENOUGH_CREDIT_QUOTA	;	# 额度不够
			}

			#
			#
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere	(
																		$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_TABLE_NAME')
																		,
																		["where id=?",[$sourceId]]
																		,
																		[
																			'credit_project_status'	=> $credit_project_status
																		]	
																	);
		}
		
		/* 审批失败
		 *
		 */
		public function approvalRefuseEnd($processInstanceId,$sourceId,$processStatus)
		{
			$this->m_cHelper->gfDbQuery()->updateByArrayWithWhere	(
																		$this->m_cHelper->gfGetTableNameByConstKey('CY_CREDIT_PROJECT_EX_TABLE_NAME')
																		,
																		["where id=?",[$sourceId]]
																		,
																		[
																			'credit_project_status'	=> modelCreditProjectManage::CREDIT_PROJECT_STATUS_6_REVIEW_REJECT
																		]	
																	);
		}
	}